package com.lagou.controller;

import com.lagou.pojo.Article;
import com.lagou.repository.ArticleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @Author: xunliu
 * @CreateTime: 21:29 2020/6/1
 * @Description:
 */
@Controller
public class ArticleController {

    @Autowired
    private ArticleRepository articleRepository;

    @RequestMapping(value = "/article")
    private String list(Model model, @RequestParam(required = false) Integer pageIndex) {
        if (pageIndex == null || pageIndex < 0) {
            pageIndex = 0;
        }
        Pageable pageable = PageRequest.of(pageIndex, 2);
        Page<Article> all = articleRepository.findAll(pageable);
        List<Article> articles = all.getContent();
        model.addAttribute("articles", articles);
        model.addAttribute("pageNo", all.getNumber());
        model.addAttribute("totalPage", all.getTotalPages() - 1);
        return "client/index";
    }
}
